package com.woyou.ui.fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.squareup.picasso.Picasso;
import com.woyou.bean.Info;
import com.woyou.bean.ShopDetail;
import com.woyou.bean.Support;
import com.woyou.ui.activity.DeliveryMapActivity_;
import com.woyou.ui.activity.ShopInfoActivity;
import com.woyou.ui.component.WordWrapView;
import com.woyou.ui.view.SuperLayout;
import com.woyou.utils.InstallUtils;
import com.woyou.utils.ParseUtils;
import com.woyou.utils.Utils;

/**
 * 店铺详情
 * 
 * @author shenzhou 2014年12月26日
 */
public class ShopInfoDetailLayout extends SuperLayout {
	
	public static final String TAG = "ShopInfoDetailLayout";

	// 控件
	private ImageView logo;
	private TextView sName;
	private TextView addrOrNotice;
	private ImageView start;
	private TextView commentsNum;
	private TextView sendPrice;
	private TextView deliver;
	private TextView sellNum;
	// private TextView AvgReceive;  //平均送达去掉暂时去掉
	private LinearLayout addrLin;
	private TextView addr;
	private LinearLayout busyTime;
	private LinearLayout phoneLin;
	private TextView phone;
	private TextView share; // 分享
	private LinearLayout noticeLin;
	private TextView notice;
	private LinearLayout infoLin;
	private WordWrapView wordWrapView; // 店铺关键字
	private TextView info;
	private TextView times;
	private LinearLayout supportLin;
	private LinearLayout supportLi;

	private ShopDetail shopDetail = null;

	public ShopInfoDetailLayout(Context context) {
		super(context);
	}

	public ShopInfoDetailLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ShopInfoDetailLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	private View view;
	private void initView() {
		if (view != null) 
			return;
		
		// 初始化控件
		view = View.inflate(getContext(), R.layout.layout_shopdetail, this);
		logo = (ImageView) view.findViewById(R.id.shopdetail_logo);
		sName = (TextView) view.findViewById(R.id.shopdetail_name);
		addrOrNotice = (TextView) view
				.findViewById(R.id.shopdetail_addrornotice);
		start = (ImageView) view.findViewById(R.id.shopdetail_start);
		commentsNum = (TextView) view.findViewById(R.id.shopdetail_comments);
		sendPrice = (TextView) view.findViewById(R.id.shopdetail_sendprice);
		deliver = (TextView) view.findViewById(R.id.shopdetail_deliver);
		sellNum = (TextView) view.findViewById(R.id.shopdetail_sellNum);
		// AvgReceive = (TextView) view.findViewById(R.id.shopdetail_sendAvg);
		addrLin = (LinearLayout) view.findViewById(R.id.shopdetail_addr);
		addr = (TextView) view.findViewById(R.id.shopdetail_addr_tx);
		phoneLin = (LinearLayout) view.findViewById(R.id.shopdetail_phone);
		phone = (TextView) view.findViewById(R.id.shopdetail_phoneTv);
		share = (TextView) view.findViewById(R.id.shopdetail_share);
		noticeLin = (LinearLayout) view
				.findViewById(R.id.shopdetail_notice_lin);
		notice = (TextView) view.findViewById(R.id.shopdetail_notice);
		infoLin = (LinearLayout) view.findViewById(R.id.shopdetail_detail_lin);
		wordWrapView = (WordWrapView) view
				.findViewById(R.id.shopdetail_container);
		info = (TextView) view.findViewById(R.id.shopdetail_info);
		times = (TextView) view.findViewById(R.id.shopdetail_times);
		supportLin = (LinearLayout) view.findViewById(R.id.shopdetail_support_lin);
		busyTime = (LinearLayout) view.findViewById(R.id.busy_time);
		supportLi = (LinearLayout) view.findViewById(R.id.shopdetail_support);

		// 设置监听器
		phoneLin.setOnClickListener(this);
		addrLin.setOnClickListener(this);
		share.setOnClickListener(this);
	}

	/**
	 * 填充数据
	 * 
	 * @param shopInfo
	 */
	public void fillData(ShopDetail shopDetail) {
		initView();
		this.shopDetail = shopDetail;
		// 加载店铺logo
		getShopLogo();

		// 加载店铺名称和门店号
		String shopName = "";
		String branch = "";
		if (!TextUtils.isEmpty(shopDetail.getsName())) {
			int indexOf = shopDetail.getsName().indexOf("(");
			if (indexOf != -1) {
				shopName = shopDetail.getsName().substring(0, indexOf);
				branch = shopDetail.getsName().substring(indexOf);
			} else {
				shopName = shopDetail.getsName();
			}
			sName.setText(shopName);
			addrOrNotice.setText(branch);
		}

		// 设置评分星级
		setStartLevel(shopDetail.getScore());

		// 评论人数
		String num = shopDetail.getCommentsNum();
		if (!TextUtils.isEmpty(num)) {
			commentsNum.setText("(" + shopDetail.getCommentsNum() + ")");
		} else {
			commentsNum.setVisibility(View.GONE);
		}

		// 营业时段
		String hours = shopDetail.getHours();
		if (!TextUtils.isEmpty(hours)) {
			// String[] split = hours.split("\\s");
			// if ( split.length > 2 ){
			// String head = split[0];
			// String[] headstr = head.split("\\-");
			//
			// String end = split[split.length-1];
			// String[] endstr = end.split("\\-");
			//
			// times.setText(headstr[0] + "到" + endstr[1] + "分为" + split.length
			// + "段营业");
			// } else {
			// times.setText(shopDetail.getHours());
			// }
			times.setText(hours);
		} else {
			busyTime.setVisibility(View.GONE);
		}

		// 店铺支持
//		if(shopDetail.getIsOnlinePay()==1){
//			supportLin.setVisibility(View.VISIBLE);
//		} else {
//			supportLin.setVisibility(View.GONE);
//		}

		// 店铺支持
		supportLi.removeAllViews();
		List<Support> list = shopDetail.getSupportList();
		if (list != null && list.size() > 0) {
			supportLin.setVisibility(View.VISIBLE);
			List<Support> supportList = shopDetail.getSupportList();
			for (Support support : supportList) {
				View child = View.inflate(getContext(),
						R.layout.item_shopdetail_support_lv, null);
				ImageView image = (ImageView) child
						.findViewById(R.id.support_image);
				TextView tv = (TextView) child.findViewById(R.id.support_text);
				Picasso.with(mActivity).load(support.getPicUrl()).into(image);
				tv.setText(support.getSuppor());
				supportLi.addView(child);
			}
		} else {
			supportLin.setVisibility(View.GONE);
		}
		
		if (!ParseUtils.String2Money(shopDetail.getToSendPrice()).equals("-1")) {
			sendPrice.setText(Html.fromHtml("￥<big><big>"
					+ ParseUtils.String2Money(shopDetail.getToSendPrice())
					+ "</big></big>"));
		} else {
			sendPrice.setTextColor(Color.RED);
			sendPrice.setText("超出范围");
		}

		if (!ParseUtils.String2Money(shopDetail.getSendFee()).equals("-1")) {
			deliver.setText(Html.fromHtml("￥<big><big>"
					+ ParseUtils.String2Money(shopDetail.getSendFee())
					+ "</big></big>"));
		} else {
			deliver.setTextColor(Color.RED);
			deliver.setText("超出范围");
		}
		sellNum.setText(Html.fromHtml("<big><big>" + shopDetail.getMonthSales()
				+ "</big></big>"));
//		AvgReceive.setText(Html.fromHtml("<big>" + shopDetail.getAvgDeliver()
//				+ "</big>分钟"));
		addr.setText(shopDetail.getAddr());
		phone.setText(shopDetail.getPhone());

		// 店铺公告
		if (!TextUtils.isEmpty(shopDetail.getNotice())) {
			noticeLin.setVisibility(View.VISIBLE);
			notice.setText(shopDetail.getNotice());
		} else {
			noticeLin.setVisibility(View.GONE);
		}

		// 店铺关键字和详情
		fullShopKeys();
	}

	/**
	 * 店铺关键字和详情
	 */
	@SuppressLint("ResourceAsColor")
	private void fullShopKeys() {
		wordWrapView.removeAllViews();
		if (!TextUtils.isEmpty(shopDetail.getInfo())) {
			infoLin.setVisibility(View.VISIBLE);
			// 店铺关键字
			String[] strs = shopDetail.getScopeList();
			for (int i = 0; i < strs.length; i++) {
				TextView textview = new TextView(mActivity);
				textview.setText(strs[i]);
				textview.setTextColor(Color.WHITE);
				wordWrapView.addView(textview);
			}
			// 店铺详情
			info.setText(shopDetail.getInfo());
		} else {
			infoLin.setVisibility(View.GONE);
		}
	}

	/**
	 * 加载店铺logo
	 */
	private void getShopLogo() {
		if ( !TextUtils.isEmpty(shopDetail.getShopPic()) ) {
			Picasso.with(mActivity).load(shopDetail.getShopPic()).into(logo);
		}
	}

	/**
	 * 设置评分星级
	 * 
	 * @param value
	 */
	private void setStartLevel(float value) {
		if (0.0 == value) {
			start.setBackgroundResource(R.raw.start0);
		} else if (1.0 >= value) {
			start.setBackgroundResource(R.raw.start0_5);
		} else if (2.0 >= value) {
			start.setBackgroundResource(R.raw.start1);
		} else if (3.0 >= value) {
			start.setBackgroundResource(R.raw.start1_5);
		} else if (4.0 >= value) {
			start.setBackgroundResource(R.raw.start2);
		} else if (5.0 >= value) {
			start.setBackgroundResource(R.raw.start2_5);
		} else if (6.0 >= value) {
			start.setBackgroundResource(R.raw.start3);
		} else if (7.0 >= value) {
			start.setBackgroundResource(R.raw.start3_5);
		} else if (8.0 >= value) {
			start.setBackgroundResource(R.raw.start4);
		} else if (9.0 >= value) {
			start.setBackgroundResource(R.raw.start4_5);
		} else {
			start.setBackgroundResource(R.raw.start5);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shopdetail_addr:
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sId", shopDetail.getsId());
			map.put("lat", shopDetail.getLat());
			map.put("lng",shopDetail.getLng());
			
			Info<Map<String, Object>> info = new Info<Map<String,Object>>();
			info.setData(map);
			info.setOrigin(ShopInfoActivity.class);
			Utils.openActivity(mActivity, DeliveryMapActivity_.class, info);
			break;
			
		case R.id.shopdetail_phone:
			callPhone();
			break;

		case R.id.shopdetail_share: // 分享
			break;
		}
	}

	public void clear() {
		if (view == null)
			return;
		logo.setBackgroundResource(R.raw.tu);
		sName.setText("");
		addrOrNotice.setText("");
		start.setBackgroundResource(R.raw.start0);
		commentsNum.setText("");
		times.setText("");
		sendPrice.setText("");
		deliver.setText("");
		sellNum.setText("");
//		AvgReceive.setText("");
		addr.setText("");
		phone.setText("");
		notice.setText("");
		info.setText("");
	}

	/**
	 * 拨打电话
	 */
	private void callPhone() {
		String number = phone.getText().toString();
		// 用intent启动拨打电话
		Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
		if(InstallUtils.isIntentAvailable(mContext, intent)){
			mContext.startActivity(intent);
		}
	}
	

}
